package com.hyyt.shopjsp.home.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.util.common.StringStaticKey;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.home.pojo.ShopHomeKeyBook;
import com.hyyt.shopjsp.home.service.IShopHomeKeyBookService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/03/22 0019下午 3:36.
 */
@Controller
@RequestMapping("/back/homeKeyBook")
public class ShopHomeKeyBookController extends BaseController {
    private Logger logger = Logger.getLogger(ShopHomeKeyBookController.class);
    private String basePath = "/back/basic/homeKeyBook/";

    @Resource
    private IShopHomeKeyBookService shopHomeKeyBookService;

    /**
	 * 进入首页数据字典管理
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoHomeKeyBookPage")
	public String gotoHomeKeyBookPage(){
		return basePath+"homeKeyBookList";
	}

	/**
	 * 查询所有首页数据字典
	 * @param page
	 * @param response
	 * @param request
	 */
	@RequestMapping(value="listHomeKeyBook",method=RequestMethod.POST)
	@ResponseBody
	public void listHomeKeyBook(Page page, HttpServletResponse response, HttpServletRequest request){
		String name=request.getParameter("name");//名称
		String typeName=request.getParameter("typeName");//类型名称
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(name)) {//是否检索名称
			param.put("name", name.trim());
		}
		if (Utils.objectIsNotEmpty(typeName)) {//是否检索类型名称
			param.put("typeName", typeName.trim());
		}
		param.put("sortColumns", "homeKeyBookId desc");
		int totalRecordCount =shopHomeKeyBookService.countShopHomeKeyBook(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopHomeKeyBook> list =shopHomeKeyBookService.queryShopHomeKeyBook(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据首页数据字典id获得对象
     *
     * @param homeKeyBookId
     * @param response
     */
    @RequestMapping(value = "getHomeKeyBookInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getHomeKeyBookInfo(Integer homeKeyBookId, HttpServletResponse response) {
		if (Utils.objectIsNotEmpty(homeKeyBookId)) {
			ShopHomeKeyBook shopHomeKeybook = shopHomeKeyBookService.loadShopHomeKeyBook(homeKeyBookId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(shopHomeKeybook));
		}else {
			return ResultMsg.create(false);
		}
	}

    /**
     * 保存或者修改用户信息
     *
     * @param shopHomeKeyBook
     * @return
     */
    @RequestMapping(value = "saveOrUpdateHomeKeyBook", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateHomeKeyBook(ShopHomeKeyBook shopHomeKeyBook) {
		if (Utils.objectIsNotEmpty(shopHomeKeyBook)) {
			if (Utils.objectIsEmpty(shopHomeKeyBook.getHomeKeyBookId())) {
				shopHomeKeyBookService.saveShopHomeKeyBook(shopHomeKeyBook);
			} else {
				shopHomeKeyBookService.updateShopHomeKeyBook(shopHomeKeyBook);
			}
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

    /**
     * 根据传入的首页数据字典id
     *
     * @param ids
     */
    @RequestMapping(value = "deleteHomeKeyBook", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteHomeKeyBook(String ids) {
		if (Utils.objectIsNotEmpty(ids)) {
			shopHomeKeyBookService.deleteShopHomeKeyBook(Integer.parseInt(ids));
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

    /**
     * 同步首页数据字典
     * @param response
     */
    @RequestMapping(value="updateInServletContextHomeKeyBook",method=RequestMethod.POST)
    @ResponseBody
	public void updateInServletContextHomeKeyBook(HttpServletResponse response){
		Map<String, Object> param=new HashMap<>();
		boolean success=false;
		List<String> typeList = shopHomeKeyBookService.getStatusRelateType();//查询要加载的数据（通过type进行了去重）
		if (Utils.collectionIsNotEmpty(typeList)) {
			for (String type : typeList) {
				param.put("type", type);
				List<ShopHomeKeyBook> homeKeyBookList = shopHomeKeyBookService.queryShopHomeKeyBook(param);   //通过type查询对应的集合
				if (Utils.collectionIsNotEmpty(homeKeyBookList)) {
					String jsonString = FastJsonUtils.toJSONString(homeKeyBookList);//将value格式化成json格式
					redisBasicService.setKey(StringStaticKey.HOME_KEYBOOK_PREFIX + type, jsonString); //将查询结果放入redis
				}
			}
			success = true;//查询到数据 将result更改为true
			param.put("success", success);
		}
		ResultMsg.create(param, response);  //把内容发送到页面
	}
}
